package com.smartleanx.module.hrm.context;

import cn.hutool.core.util.StrUtil;
import com.alibaba.ttl.TransmittableThreadLocal;

/**
 * 员工上下文 Holder
 *
 * @author tengcj
 */
public class EmployeeContextHolder {

    /**
     * 当前员工编号
     */
    private static final ThreadLocal<Long> EMPLOYEE_ID = new TransmittableThreadLocal<>();

    /**
     * 获得员工编号
     *
     * @return 员工编号
     */
    public static Long getEmployeeId() {
        return EMPLOYEE_ID.get();
    }

    /**
     * 获得员工编号 String
     *
     * @return 租户编号
     */
    public static String getEmployeeIdStr() {
        Long employeeId = getEmployeeId();
        return StrUtil.toStringOrNull(employeeId);
    }

    /**
     * 获得员工编号。如果不存在，则抛出 NullPointerException 异常
     *
     * @return 员工编号
     */
    public static Long getRequiredEmployeeId() {
        Long employeeId = getEmployeeId();
        if (employeeId == null) {
            throw new NullPointerException("EmployeeContextHolder 不存在员工编号！");
        }
        return employeeId;
    }

    public static void setEmployeeId(Long employeeId) {
        EMPLOYEE_ID.set(employeeId);
    }

    public static void clear() {
        EMPLOYEE_ID.remove();
    }

}
